﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
namespace phtoreader
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
        }
        public class photoFileDirInfo
        {
            public string currentDir;
            public string currentFile;
            public string[] currenDirFiles;
            public int currentDirFileCount;
            public int currentFileNumber;

            
        }
        photoFileDirInfo dirInfo = new photoFileDirInfo();
        
        public string openFile()
        {
            Microsoft.Win32.OpenFileDialog openFile = new Microsoft.Win32.OpenFileDialog();
            //openFile.InitialDirectory = "D://LCPfiles//TASKS//23suo_highsppedAdc//";
            openFile.Filter = "jpg Flile(*.jpg)|*.jpg|PNG Flile(*.png)|*.png";
            //openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            openFile.Multiselect = false;
            if (openFile.ShowDialog() == false) return "err";
            var filePath = openFile.FileName;
            
            string fileType = System.IO.Path.GetExtension(filePath);
            if (string.IsNullOrEmpty(fileType)) return "err";
            FileInfo fileinfo = new FileInfo(openFile.SafeFileName);

            dirInfo.currentDir = System.IO.Path.GetDirectoryName(filePath);
           // dirInfo.currentFilePath = System.IO.Path.GetFileNameWithoutExtension(filePath);
            
            string[] files = System.IO.Directory.GetFiles(dirInfo.currentDir, "*.*");
            dirInfo.currenDirFiles = files;
            dirInfo.currentDirFileCount = files.Length;
            //计算当前位置
            dirInfo.currentFileNumber = 0;
            dirInfo.currentFile = filePath;
            foreach (string filename in files)
            {
                if (filename == filePath) break;
                else
                    dirInfo.currentFileNumber += 1;

            
            }
            return filePath;
        }
        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            windw1.Left = 0;
            windw1.Top = 0;
            
        }
        
        private void windw1_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var input = e.Key;
            //方向键左右分别为前一页和后一页
            if (input == Key.Right && dirInfo.currentFileNumber < dirInfo.currentDirFileCount)
            {
                dirInfo.currentFileNumber += 1;
                loadJPG(dirInfo.currenDirFiles[dirInfo.currentFileNumber]);
            
            }
            if (input == Key.Left && dirInfo.currentFileNumber>0)
            {
                dirInfo.currentFileNumber -= 1;
                loadJPG(dirInfo.currenDirFiles[dirInfo.currentFileNumber]);

            }
        }
        public void loadJPG(string filePath)
        {
            BitmapImage image1 = new BitmapImage();
            image1.BeginInit();
            image1.UriSource = new Uri(filePath, UriKind.Absolute);
            image1.EndInit();
            double img_width = image1.Width;
            double img_height = image1.Height;
            //if (img_height > (1080 - 100)) img_height = 980;
            imagebox1.Source = image1;
            imagebox1.Width = img_width;
            imagebox1.Height = img_height;
        
        }
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string filePath = openFile();
            if (filePath != "err") 
                loadJPG(filePath);
            
        }

        private void ScrollViewer_PreviewMouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
            Point pp = e.GetPosition(imagebox1);
            if (pp.X > imagebox1.Width*0.66)
            {
                if ( dirInfo.currentFileNumber < dirInfo.currentDirFileCount-1)
                {
                    dirInfo.currentFileNumber += 1;
                    loadJPG(dirInfo.currenDirFiles[dirInfo.currentFileNumber]);

                }
            }
            if (pp.X < imagebox1.Width * 0.33)
            {
                if (dirInfo.currentFileNumber >0)
                {
                    dirInfo.currentFileNumber -= 1;
                    loadJPG(dirInfo.currenDirFiles[dirInfo.currentFileNumber]);

                }
            }
            //Rectangle rectangle = sender as Rectangle;
            //Point point = rectangle.TranslatePoint(pp, scrollview1); 
        }

       
        
    }
}
